package com.bbs.controller;

import com.bbs.entity.*;
import com.bbs.service.PostService;
import com.bbs.service.PostUserService;
import com.bbs.service.ReplyService;
import com.bbs.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private PostUserService postUserService;

    //去帖子详情页面
    @RequestMapping("/toPost")
    public String toPost(int pid, Model model, HttpSession session){
        Integer sessionUid = (Integer) session.getAttribute("uid");
        //获取帖子信息
        Post post = postService.getPostByPid(pid);
        //获取评论信息
        List<Reply> replyList = replyService.listReply(post.getId());
        // 用户是否点赞
        boolean liked = false;
        if(sessionUid != null){
            PostUser postUser = postUserService.findByPidAndUid(pid,sessionUid);
            if(postUser != null){
                liked = true;
            }
        }

        //向模型中添加数据，key（第一个参数）为post和replyList
        model.addAttribute("post",post);
        model.addAttribute("replyList",replyList);
        model.addAttribute("liked",liked);// 用户是否点过赞，点过就是true,否则false
        return "post";
    }

    //去发帖的页面
    @RequestMapping("/toPublish")
    public String toPublish(Model model,HttpSession session){
        if(session.getAttribute("uid") == null){
            return "login";
        }
        List<Topic> topicList = topicService.listTopic();
        model.addAttribute("topicList",topicList);// 给模板添加数据
        return "publish";
    }

    /**
     * 发帖
     * @param post
     * @return
     */
    @RequestMapping("/publishPost")
    @ResponseBody
    public ResultInfo publishPost(@RequestBody Post post){
        System.out.println(post);
        int flag = postService.publishPost(post);
        System.out.println("发帖结果："+flag);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setMsg("发布成功");
        resultInfo.setCode("0");
        return resultInfo;
    }

    /**
     * 回帖/评论
     * @return
     */
    @RequestMapping("/reply")
    @ResponseBody
    public ResultInfo reply(@RequestBody Reply reply, HttpSession session){
        System.out.println(reply);
        ResultInfo resultInfo = new ResultInfo();
        // 下面这行未登录下不会报错
        Integer uid = (Integer) session.getAttribute("uid");
        if(uid != null){
            reply.setUid(uid);
            int flag = replyService.reply(reply);
            if(flag == 1){
                resultInfo.setCode("0");
                resultInfo.setMsg("评论成功");
            }else {
                resultInfo.setCode("1");
                resultInfo.setMsg("评论失败");
            }
        }else {
            resultInfo.setCode("2");
            resultInfo.setMsg("评论失败，请登录");
        }
        return resultInfo;
    }

    /**
     * 点赞
     */
    @RequestMapping("/like")
    @ResponseBody
    public ResultInfo like(int pid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo();
        int uid = (int)session.getAttribute("uid");
        postUserService.save(new PostUser(pid,uid));
        Post post = postService.getPostByPid(pid);
        resultInfo.setCode(""+post.getLikeCount());
        resultInfo.setMsg("已赞");

        return resultInfo;
    }

    /**
     * 楼中楼评论
     * @param pid
     * @param rid
     * @param content
     * @param session
     * @return
     */
    @RequestMapping("/comment")
    public String comment(int pid,int rid, String content, HttpSession session){
        Integer uid = (Integer)session.getAttribute("uid");
        if(uid == null){
            return "redirect:toLogin";
        }
        int sessionUid = (int) session.getAttribute("uid");
        replyService.comment(pid,sessionUid,rid,content);
        return "redirect:toPost.do?pid="+pid;
    }


}
